package com.carboncntr.helper;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.carboncntr.tables.Customer;
import com.carboncntr.util.HibernateUtil;

public class CustomerHelper {
	public final static Long saveCustomer(String customerFirst,String customerLast,String customerUserName,String customerPassword,String customerEmail, String customerZip){
		Session session=HibernateUtil.getSessionfactory().openSession();
		Transaction transaction=null;
		Long customerId=null;
		try {
			transaction=session.beginTransaction();
			Customer customer=new Customer();
			
			customer.setFirst(customerFirst);
			customer.setLast(customerLast);
			customer.setEmail(customerEmail);
			customer.setPassword(customerPassword);
			customer.setUserName(customerUserName);
			customer.setZip(customerZip);
			customerId=(Long) session.save(customer);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		
		return customerId;
	}
	
	public final static Customer updateCustomer(Long customerId, String customerFirst,String customerLast, String customerPassword,String customerEmail, String customerZip){
		Session session=HibernateUtil.getSessionfactory().openSession();
		Transaction transaction=null;
		Customer customer = null;
		try {
			transaction=session.beginTransaction();
			customer= (Customer) session.get(Customer.class, customerId); 
			
			customer.setFirst(customerFirst);
			customer.setLast(customerLast);
			customer.setEmail(customerEmail);
			customer.setPassword(customerPassword);
			customer.setZip(customerZip);
			customerId=(Long) session.save(customer);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return customer;
	}
	
	public final static Customer getCustomerByUserName(String userName) { 
		Session session = HibernateUtil.getSessionfactory().openSession();
		Customer c = null;
		try { 
			List<Customer> customers = session.createQuery("from Customer where username = '" + userName + "'").list(); 
			if(customers != null && customers.size() > 0) {
				c = customers.get(0);
			}
		} catch (HibernateException e) { 
			e.printStackTrace(); 
		} finally { 
			session.close(); 
		} 
		return c;
	}
	
	public final static Customer getCustomerLogin(String userName, String password) { 
		Session session = HibernateUtil.getSessionfactory().openSession();
		Customer c = null;
		try { 
			List<Customer> customers = session.createQuery("from Customer where username = '" + userName + "' and password= '" + password + "'").list(); 
			if(customers != null && customers.size() > 0) {
				c = customers.get(0);
			}
		} catch (HibernateException e) { 
			e.printStackTrace(); 
		} finally { 
			session.close(); 
		} 
		return c;
	}
	
	public final static Customer getCustomerById(Long customerId) { 
		Session session = HibernateUtil.getSessionfactory().openSession();   
		Customer customer = null;
		try {             
			customer= (Customer) session.get(Customer.class, customerId); 
		} catch (HibernateException e) {             
			e.printStackTrace(); 
		} finally { 
			session.close();     
		} 
		return customer;
	}

	public final static void listCustomer() 
	{ 
		Session session = HibernateUtil.getSessionfactory().openSession();
		Transaction transaction = null; 
		try { 
			transaction = session.beginTransaction();           
			List customers = session.createQuery("from Customer").list(); 

			for (Iterator iterator = customers.iterator(); iterator.hasNext();){ 
				Customer customer= (Customer) iterator.next(); 
				System.out.println(customer.getFirst()); 
			} 
			transaction.commit(); 
		} catch (HibernateException e) { 
			transaction.rollback(); 
			e.printStackTrace(); 
		} finally { 
			session.close(); 
		} 
	}
	
	public final static void updateCustomer(Long customerId, String customerFirst) { 
		Session session = HibernateUtil.getSessionfactory().openSession();         
		Transaction transaction = null;         
		try {             
			transaction = session.beginTransaction();         
			Customer customer= (Customer) session.get(Customer.class, customerId); 
			customer.setFirst(customerFirst); 
			transaction.commit();     
		} catch (HibernateException e) {             
			transaction.rollback();        
			e.printStackTrace(); 
		} finally { 
			session.close();     
		} 
	}
	public final static void deleteCustomer(Long customerId){
		Session session = HibernateUtil.getSessionfactory().openSession(); 
		Transaction transaction = null; 
	        
		try {             
			transaction = session.beginTransaction();         
			Customer customer= (Customer) session.get(Customer.class, customerId); 
			session.delete(customer); 
			transaction.commit();     
		} catch (HibernateException e) {             
			transaction.rollback();        
			e.printStackTrace();     
		} finally {             
			session.close(); 	    
		}     
	} 

}
